(function e(t, n, r) {
    function s(o, u) { if (!n[o]) { if (!t[o]) { var a = typeof require == "function" && require; if (!u && a) return a(o, !0); if (i) return i(o, !0); var f = new Error("Cannot find module '" + o + "'"); throw f.code = "MODULE_NOT_FOUND", f } var l = n[o] = { exports: {} };
            t[o][0].call(l.exports, function(e) { var n = t[o][1][e]; return s(n ? n : e) }, l, l.exports, e, t, n, r) } return n[o].exports } var i = typeof require == "function" && require; for (var o = 0; o < r.length; o++) s(r[o]); return s })({
    1: [function(require, module, exports) {
        module.exports = (function() {
            if (!window.Zepto && !window.jQuery) {
                throw 'need Zepto || jQuery';
            }

            var startTime = +new Date;
            var djData = {};

            function param(obj) {
                var buffer = [];

                for (var key in obj) {
                    buffer.push(key + '=' + encodeURIComponent(obj[key]));
                }

                return buffer.join('&');
            };

            function init(data) {
                data.forEach(function(item, index) {
                    if (item['loginfo']) {
                        item['loginfo']['startTime'] = +new Date;
                        item['loginfo']['index'] = index;
                        djData[item.apkid] = item.loginfo;
                    }
                })
            }

            function getDjData(id) {
                if (djData[id]) {
                    return true;
                } else {
                    return false;
                }
            }

            var sendLog = (function() {
                var queue = []; // 请求队列
                var isLoading = false;
                var Logger = {

                    /**
                     * 获取统计URL
                     *
                     * @param {string} sid, 软件ID
                     * @param {string} act, start=下载，show=pv打点  browse=点击打点
                     * @param {string} pos, 位置
                     */
                    getLogUrl: function(type, data) {
                        if (type == 'ck') {
                            data = djData[data];
                        }
                        var serverDefault = {
                            'pv': 'http://e.tf.360.cn/b/pv',
                            'ck': 'http://e.tf.360.cn/b/click'
                        };
                        var server = serverDefault[type];
                        var time = +new Date;

                        var obj = {
                            mid: qStore.util.getQuery('m') || '',
                            m2: qStore.util.getQuery('m2') || qStore.util.getMID(),
                            st: +new Date,
                            asin: data['asin']
                        }

                        if (type == 'pv' && Object.prototype.toString.call(data) == '[object Array]') {
                            var res = [];
                            data.forEach(function(item) {
                                res.push({
                                    asin: item.asin,
                                    adindex: item.index,
                                })
                            });
                            obj['asin'] = JSON.stringify(res);
                        }

                        if (type == 'ck') {
                            obj['st'] = data.startTime;
                            obj['ct'] = +new Date;
                            obj['ut'] = +new Date + 300;
                            obj['adindex'] = data.index;
                            obj['model'] = qStore.util.getQuery('md');
                            obj['from'] = 2;
                        }
                        return server + '?' + $.param(obj);
                    },

                    request: function(url) {
                        if (!this.hamal) {
                            var hamal = this.hamal = document.createElement('img');

                            hamal.style.visibility = 'hidden';
                            hamal.style.width = hamal.style.height = '1px';
                            hamal.style.position = 'absolute';
                            hamal.style.left = '-1000px';
                            document.body.appendChild(hamal);

                            hamal.onload = hamal.onerror = function() {
                                isLoading = false;
                                if (queue.length > 0) {
                                    Logger.request(queue.shift());
                                }
                            };
                        }

                        if (isLoading) {
                            queue.push(url);
                            return;
                        }
                        isLoading = true;
                        this.hamal.src = url;
                    },

                    record: function() {
                        var logUrl = this.getLogUrl.apply(this, arguments);

                        this.request(logUrl);
                    }
                };

                function record() {
                    return Logger.record.apply(Logger, arguments);
                };

                return record;
            })();

            return {
                init: init,
                getDjData: getDjData,
                sendLog: sendLog,
            }
        })();
    }, {}],
    2: [function(require, module, exports) {

        var tmpl = require('./tmpl.js');
        var tpl = require('./tpl3.js');
        var dianjing = require('./dianjing.js');
        // var dj = require('./dj.js');

        window.formatSize = function(e) {
            if (e == 0) return "0K";
            var t = "BKMGTP",
                n = Math.log(e) / Math.LN2 / 10,
                r = Math.floor(n + .004);
            return Math.pow(1024, n - r).toPrecision(3) + t[r];
        }

        var REQUEST_URL = '/home/access';
        var REQUEST_TYPE = 'json';
        if (location.host == '' || location.host.indexOf('test') > -1) {
            REQUEST_URL = 'http://test1.baohe.mobilem.360.cn/home/access';
            REQUEST_TYPE = 'jsonp';
        }

        var App = {
            init: function() {
                App.initPageData();
                App.bindEvents();
            },

            getData: function() {
                var data = {
                    gender: 1,
                    age: 25,
                    page: 1,
                    _t: +new Date
                };

                return $.ajax({
                    url: REQUEST_URL + location.search,
                    type: 'get',
                    dataType: REQUEST_TYPE,
                    data: data
                })
            },

            renderTpl: function(data) {
                var res = [];
                var appData = [];
                var djArray = [];

                var tagArray = window.tagArray || {

                };

                data.forEach(function(item, index) {
                    item['domId'] = 'App_' + index;
                    if (index % 2 == 0) {
                        if (item.tags) {
                            var tag = item.tags.split(';') || '';
                            tag = tag[0] || '';
                            if (tagArray[tag]) {
                                item.pic = tagArray[tag];
                            }
                        }
                        res.push(tmpl(tpl['tpl1'], { data: item }));
                    } else {
                        res.push(tmpl(tpl['tpl2'], { data: item }));
                    }
                    item.apps.forEach(function(info) {
                        if (info['loginfo']) {
                            info['loginfo']['startTime'] = +new Date;
                            info['loginfo']['index'] = index;
                            djArray.push(info['loginfo']);
                        }
                        if (info['id']) {
                            info['soft_id'] = info['id'];
                        }
                        appData.push(info);
                    })
                });
                dianjing.init(appData);
                dianjing.sendLog('pv', djArray);
                qStore.app.addData(appData);

                res = res.join('');
                $('.wrap').append(res);
                qStore.web.checkAppStatus();
                var sd = ScrollerDetector.init();
                sd.check();
            },

            initPageData: function() {
                var data = window.ZXDATA;
                var m2 = qStore.util.getQuery("m2") || qStore.util.getMID();
                var flag = String(m2).charCodeAt(String(m2).length - 1) % 4; //flag 0 1 2 3
                /**
                 * flag 根据m2区分 
                 * 0：页面原来的设计
                 * 1：设计图 样式 1
                 * 2：设计图 样式2
                 * 3：ln提的样式
                 */
                window.pos='color_change'+flag;
                qStore.util.record("","show",window.pos);
                if (data.errno == 0) {
                    var result = data.data.list;
                    if (result.push && result.length > 0) {
                        App.renderTpl(result);
                    } else {
                        console.log('数据为空');
                    }
                } else {
                    console.log('数据为空');
                }
            },

            bindEvents: function() {
                try {
                    AndroidWebview.setScrollbackEnable(false);
                } catch (e) {}

                $('.wrap').on('click', '.is-download', function(e) {
                    var id = $(this).closest('.js-app-item').data('apkid');
                    var sid = $(this).closest('.js-app-item').data('sid');
                    qStore.util.record(sid, "start", window.pos);

                    if (dianjing.getDjData(id)) {
                        dianjing.sendLog('ck', id);
                    }
                })
            }
        }
        App.init();
    }, { "./dianjing.js": 1, "./tmpl.js": 3, "./tpl3.js": 4 }],
    3: [function(require, module, exports) {
        module.exports = (function() {
            return (function() {
                var cache = {};

                function compile(str) {
                    return "var __result__='';" +
                        "__result__+='" +
                        str
                        .replace(/[\r\t\n]/g, " ")
                        .split("<%").join("\t")
                        .replace(/((^|%>)[^\t]*)'/g, "$1\r")
                        .replace(/\t=(.*?)%>/g, "'+$1+'")
                        .split("\t").join("';")
                        .split("%>").join("__result__+='")
                        .split("\r").join("\\'") +
                        "';return __result__";
                }
                return function(str, data) {
                    if (!str) {
                        throw '模板字符串为空'
                    }
                    if (!data) {
                        throw '渲染数据为空'
                    }
                    // 第一层缓存，使用模板作为缓存标识
                    var tplObj = cache[str];
                    if (!tplObj) {
                        tplObj = cache[str] = {
                            fnBody: compile(str),
                            fnCache: {}
                        };
                    }

                    var keys = [],
                        values = [];
                    for (var k in data) {
                        if (data.hasOwnProperty(k)) {
                            keys.push(k);
                            values.push(data[k]);
                        }
                    }

                    // 第二层缓存，使用属性名作为缓存标识
                    var cacheKey = keys.toString(),
                        fn = tplObj.fnCache[cacheKey];
                    if (!fn) {
                        fn = tplObj.fnCache[cacheKey] = new Function(keys, tplObj.fnBody);
                    }

                    return fn.apply(this, values);
                };
            })();
        })();
    }, {}],
    4: [function(require, module, exports) {
        module.exports = (function() {


            var tpl1 = [
                '        <% ',

                '                var tags = data.tags ? data.tags.split(\';\') : [];',
                '                tag = tags.length > 0 ? tags[0] + \'类\' : \'\';',
                '                var child = data.apps;',
                '                var tagImg = data.pic ? data.pic : false;',
                '                var domId = data.domId;',
                '                var topic_id = data.topic_id;',
                '                var index = data.domId.substring(data.domId.indexOf("_")+1);',
                '                var title =data.title;',
                'var m2 = qStore.util.getQuery("m2") || qStore.util.getMID();',
                'var  color_flag=String(m2).charCodeAt(String(m2).length-1)%4;',

                '        %>',
                '        <li id="<%=domId%>" class="app-block block-pink <% if(tagImg) { %>pic-bg<% } %>" <% if (tagImg) { %> style="background-image: url(<%=tagImg%>)" <% } %>>',
                '            <div class="block-title">',
                '                <h2 class="f16 els"><%=title%></h2>',
                '                <p class="els">最受欢迎的<%=tag%>App >></p>',
                '            </div>',
                '            <section>',
                '                <% ',
                '                    for (var j=0; j < child.length; j++) {',
                '                        if (j > 2) { break; }',
                '                        var apkid = child[j][\'apkid\'];',
                '                        var logo_url = child[j][\'logo_url\'];',
                '                        var single_word = child[j][\'single_word\'];',
                '                        var name = child[j][\'name\'];',
                '                        var downTimes = child[j][\'download_times\'];',
                '                        var user = downTimes >= 10000 ? Math.floor(downTimes/10000) +\'万\' : downTimes;',
                '                        var fileSize = formatSize(child[j][\'size\']);',
                '                        var childDomId = domId + "_" + j;',
                '                        var sid = child[j][\'id\'];',

                '                %>',
                '                <div class="app-item js-app-item" id="<%=childDomId%>" data-sid="<%=sid %>" data-apkid="<%=apkid%>">',
                '                    <div class="app-logo">',
                '                        <img class="w100" src="http://p5.qhimg.com/d/inn/d88467b0/default_download.png" data-icon="<%=logo_url%>">',
                '                    </div>',
                '                    <div class="app-main">',
                '<% if(color_flag==0){ %>',
                '                        <div class="app-detail">',
                '                            <h3 class="f13 els"><%=single_word%></h3>',
                '                            <div class="app-info els f16">',
                '                                <%=name%>',
                '                            </div>',
                '                            <a href="javascript:void(0);" class="app-btn is-download btn-pos btn-pink js-app-btn">下载</a>',
                '                        </div>',
                '                        <div class="app-tail">',
                '                            <span class="app-info1"><%=user%>人在用</span>',
                '                            <span class="app-info2"><%=fileSize%></span>',
                '                        </div>',

                '<% }else if(color_flag==1){ %>',
                '                        <div class="app-detail" id="color_flag1">',
                '                            <h3 class="f16 els "><%=single_word%></h3>',
                '                            <div class="app-info els f13">',
                '                                <%=name%>',
                '                            </div>',
                '                            <a href="javascript:void(0);" class="app-btn is-download btn-pos btn-pink js-app-btn">下载</a>',
                '                        </div>',
                '                        <div class="app-tail">',
                '                            <span class="app-info1"><%=user%>人在用</span>',
                '                            <span class="app-info2"><%=fileSize%></span>',
                '                        </div>',

                '<% }else if(color_flag==2){ %>',

                '                        <div class="app-detail " id="color_flag2">',
                '                            <h3 class="f16 els"><%=name%></h3>',
                '                            <div class="app-info els f13 ">',
             
                '                            <span class="app-info1"><%=user%>人在用</span>',
                '                            <span class="app-info2"><%=fileSize%></span>',
                '                            </div>',
                '                            <a href="javascript:void(0);" class="app-btn is-download btn-pos btn-pink js-app-btn">下载</a>',
                '                        </div>',
                '                        <div class="app-tail f14 els">',
                   '                                <%=single_word%>',
                '                        </div>',

                '<% }else if(color_flag==3){ %>',

                '                        <div class="app-detail" id="color_flag1">',
                '                            <h3 class="f16 els " ><%=single_word%></h3>',
                '                            <div class="app-info els f13 ">',
                '                                <%=name%>',
                '                            </div>',
                '                            <a href="javascript:void(0);" class="app-btn is-download btn-pos btn-pink js-app-btn">下载</a>',
                '                        </div>',
                '                        <div class="app-tail color_flag1">',
                '                            <span class="app-info1"><%=user%>人在用</span>',
                '                            <span class="app-info2"><%=fileSize%></span>',
                '                        </div>',

                '<% } %>',

                '                    </div>',
                '                </div>',
                '                <%',
                '                    }',
                '                %>',
                '            </section>',
                '        </li>'
            ].join("");

            var tpl2 = [
                '        <% ',

                '                var tags = data.tags ? data.tags.split(\';\') : [];',
                '                tag = tags.length > 0 ? tags[0] + \'类\' : \'\';',
                '                var child = data.apps;',
                '                var tagImg = data.pic ? data.pic : false;',
                '                var domId = data.domId;',
                '                var index = data.domId.substring(data.domId.indexOf("_")+1);',

                '                var title =data.title;',
                '        %>',
                '        <li id="<%=domId%>" class="app-scroll">',
                '            <div class="block-title">',
                '                <h2 class="f16 els"><%=title%></h2>',
                '                <p class="els">最受欢迎的<%=tag%>App >></p>',
                '            </div>',
                '            <section class="scroll-wrap">',
                '                <ul>',
                '                    <% ',
                '                        for (var j=0; j < child.length; j++) {',
                '                            var apkid = child[j][\'apkid\'];',
                '                            var logo_url = child[j][\'logo_url\'];',
                '                            var single_word = child[j][\'single_word\'];',
                '                            var name = child[j][\'name\'];',
                '                            var downTimes = child[j][\'download_times\'];',
                '                            var user = downTimes >= 10000 ? downTimes/10000 +\'万\' : downTimes;',
                '                            var fileSize = formatSize(child[j][\'size\']);',
                '                            var childDomId = domId + "_" + j;',
                '                        var sid = child[j][\'id\'];',
                '                    %>',
                '                    <li class="app-scroll-wrap app-item js-app-item" id="<%=childDomId%>"  data-sid="<%= sid%>" data-apkid="<%=apkid%>">',
                '                        <div class="app-logo">',
                '                            <img class="w100" src="http://p5.qhimg.com/d/inn/d88467b0/default_download.png" data-icon="<%=logo_url%>">',
                '                        </div>',
                '                        <h3 class="els f14"><%=name%></h3>',
                '                        <p class="els f12"><%=single_word%></p>',
                '                        <a href="javascript:void(0);" class="app-btn btn-pink is-download js-app-btn">下载</a>',
                '                    </li>',
                '                    <%',
                '                        }',
                '                    %>',
                '                </ul>',
                '            </section>',
                '        </li>'
            ].join("");

            return {
                tpl1: tpl1,
                tpl2: tpl2
            }
        }());
    }, {}]
}, {}, [2]);